https://leetcode.cn/problems/matrix-block-sum/description/
class Solution {
public:
    vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {
        int m=mat.size(),n=mat[0].size();
        vector<vector<int>> dp(m+1,vector<int>(n+1));
        vector<vector<int>> ret(m,vector<int>(n));
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
                dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1]+mat[i-1][j-1];
        for(int row=0;row<m;row++)
        {
            for(int col=0;col<n;col++)
            {
                //防止越界,并且建立下标映射
                int x1=max(0,row-k)+1,y1=max(0,col-k)+1;
                int x2=min(m-1,row+k)+1,y2=min(n-1,col+k)+1;
                ret[row][col]=dp[x2][y2]-dp[x1-1][y2]-dp[x2][y1-1]+dp[x1-1][y1-1];
            }
        }
        return ret;
    
    }
};